<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>

</head>
<body>
<ul id="ul001">
     <!--<li>某某产品<label>价格：</label><input type="button" value="-"/><label>0</label><input type="button" value="+"/></li>-->
</ul>

总价格：<input type="text" id="lblall" value="0"/>
<script src="/js/jquery.js"></script>
<script>
    $(document).ready(function () {
        $.ajax({
            url: '/getAddress',
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            async: false,
            success: function(data){
                if(data.msgCode="000000")
                {
                    $.each(data.data, function(i, item){
                        $("#ul001").append("<li id=li"+item.shoppingId+"><img src="+item.imgUrl+">"+item.productName+" "+item.proColor+"--价格：<label>"+item.productPrice+"" +
                            "</label><input onclick='setNumber(this,1,"+item.shoppingId+");' type='button' value='-'/><label id=lbl"+item.shoppingId+">"+item.productCount+"</label>" +
                            "<input onclick='setNumber(this,2,"+item.shoppingId+");'  type='button' value='+'/><input type='button' onclick='deleteshoping("+item.shoppingId+")' value='删除'></li>");
                    });
                }
                else
                {
                    alert("获取失败!");
                }
            }
        });
    });
    function deleteshoping(shoppingId) {
        $.ajax({
            url: '/deleteCart?shoppingId='+shoppingId,
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            async: false,
            success: function(data){
                if(data.data>0)
                {
                    alert("删除成功!");
                    getPrice();
                    window.location=location;
                }
                else
                {
                    alert("删除失败!");
                }
            }
        });
    }
    function getPrice() {
        var all=0;
        $.each($("#ul001 li"),function (i,item) {
            all+=$.trim($(this).find("label").eq(0).html())*$.trim($(this).find("label").eq(1).html());
        })
        $("#lblall").val(all);
    }
    function setNumber(obj,type,shoppingId) {
        var oldValue=parseInt($("#lbl"+shoppingId).html());
        if(type==1)
        {
            if(oldValue==1)
            {
                $("#lbl"+shoppingId).html(1);
                getPrice();
            }
            else
            {
                var nowValue=oldValue-1;
                var res=setValue(shoppingId,nowValue);
                if(res>0) {
                    $("#lbl" + shoppingId).html(nowValue);
                    getPrice();
                }
                else
                {
                    alert("添加失败!");
                }
            }
        }
        else if(type==2) {
            var nowValue=oldValue+1;
            var res=setValue(shoppingId,nowValue);
            if(res>0) {
                $("#lbl" + shoppingId).html(nowValue);
                getPrice();
            }
            else
            {
                alert("添加失败!");
            }
        }
    }

    function setValue(shoppingId,value) {
        var res=0;
        $.ajax({
            url: '/updateCart?shoppingId='+shoppingId+"&number="+value,
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            async: false,
            success: function(data){
                if(data.data>0)
                {
                    res=data.data;
                }
            }
        });
        return res;
    }
</script>
</body>
</html>